
var canvas = document.getElementById('Canvas');
var ctx = canvas.getContext('2d');


	var Width = window.innerWidth;//420
	var Height = window.innerHeight;//550
	var hurdleCount = 3;
	var hurdles=[];
	var position = 0;
	var speed=15;
	var Pause=0;
	var score=0.0;
	var Gspeed=100;
	var ScoreMult=1;
	var Level = 1;
	var EarthCount =0;




canvas.width=Width;
canvas.height = Height;

var _image= document.getElementById('Sprite');
var _obs1= document.getElementById('Obs1');
var _obs2= document.getElementById('Obs2');
var _obs3= document.getElementById('Obs3');
var _obs4= document.getElementById('Obs4');
var _obs5= document.getElementById('Obs5');
var _shipFlame= document.getElementById('Ship_flame');
var _ship= document.getElementById('Ship');
var _earth= document.getElementById('Earth');

//actor class 
var Actor = function(){


	this.width=(7*10);
	this.height=(7*17.1);

	this.cx = 0;
	this.cy=0;
	this.cwidth = 100;
	this.cheight = 171;


	
	//position actor at the middle

	this.x = Width / 2 - this.width / 2;
	this.y = 44;

	
	this.vx=20;

	this.draw = function() {
		try {
			// ctx.drawImage(_image, this.cx, this.cy, this.cwidth, this.cheight, this.x, this.y, this.width, this.height);
			ctx.drawImage(_shipFlame,this.cx, this.cy, this.cwidth, this.cheight, this.x, this.y, this.width, this.height);
		} catch (e) {
			console.log(e);
		}
	};
}
//create actor object of type Actor
actor = new Actor();

//may do later
 var actor_vy = Height - (actor.height); 
 actor.y= actor_vy;
// actor.draw();
// adding inputs...
document.onkeyup = function(e) 
{
	var key = e.keyCode;

		if (key == 37) 
		{
			actor.x -= actor.vx;

			
		} 
		else if (key == 39) 
		{
			actor.x += actor.vx;
			
			
		}
		else if (key==32) //press space to pause
		{
			if(Pause==0)
				{
					Pause=1;
				}
			else if(Pause==1)Pause=0;
			
		};
};

document.onkeydown = function(e) {
		var key = e.keyCode;

		if (key == 37) {
			actor.x -= actor.vx;
		} else if (key == 39) {
			actor.x += actor.vx;
		}else if (key==32) {
         updateHurdles()
		};
	};

// obstruction class

Hurdle = function(){
	this.width = 68;
	this.height = 63;

	this.x = Math.random() * (Width - this.width);
	this.y = position;

	this.rand = Math.floor(Math.random()*10);


	position += (Height / hurdleCount);

	

	//Sprite clipping
	this.cx = 0;
	this.cy = 0;
	this.cwidth = 98;
	this.cheight = 93;

	//Function to draw it
	this.draw = function() {
		try {
			 if(this.rand == 9 || this.rand == 7)//draw obs 4
			 {
				
				ctx.drawImage(_earth, this.cx, this.cy, this.cwidth, this.cheight, this.x, this.y, this.width, this.height);
			 }
			if(this.rand == 4)//draw obs 4
			 {
				
			 	ctx.drawImage(_obs4, this.cx, this.cy, this.cwidth, this.cheight, this.x, this.y, this.width, this.height);
			 }
			if(this.rand== 5)//draw obs 5
			{
				ctx.drawImage(_obs5, this.cx, this.cy, this.cwidth, this.cheight, this.x, this.y, this.width, this.height);
			}
			if( this.rand == 3)//draw obs 3
			{
					ctx.drawImage(_obs3, this.cx, this.cy, this.cwidth, this.cheight, this.x, this.y, this.width, this.height);
			}
			if (this.rand == 2) //draw obs 2
			 	{
			 		ctx.drawImage(_obs2, this.cx, this.cy, this.cwidth, this.cheight, this.x, this.y, this.width, this.height);
			 	}
			 else if(this.rand ==1 ||this.rand ==6 ||this.rand ==8 ||this.rand ==0 )// draw obs 1
			 {
				ctx.drawImage(_obs1, this.cx, this.cy, this.cwidth, this.cheight, this.x, this.y, this.width, this.height);
			 }
			
		} catch (e) {
			console.log(e);
		}
	};
	this.rub = function()
	{
		ctx.clearRect(this.x,this.y,this.width,this.height);
	}

	
	 this.vx = 1;

}
	 //draw hurdle initially
	 
var drawHurdle =function()
{
	for(var i = 0; i < hurdleCount; i++) {
		hurdles[i]= new Hurdle();
 		//hurdles[i].draw();
 	}
 }

function loop () {
	ctx.clearRect(0, 0, Width, Height);
	for (var i = 0; i < hurdleCount; i++) {
 	actor.draw();
 	hurdles[i].draw();
 	score=score+1;
 	//showScore();
 	console.log(score);
 	if(score % (ScoreMult*1500) == 0)
 	{
 		//Gspeed=Gspeed+50;
 		gameRefresh();
 		//console.log('refreshed');
 		//gameStart();
 		
 	}

 }
}

function updateHurdles()

{
	if(Pause==0){
		hurdles.forEach(function(p, i) 
		{
				
				p.y += 15.00;
				
				if (p.y > Height) {
					hurdles[i] = new Hurdle();
					hurdles[i].y = p.y - Height;
				}
				if(p.y >= Height - (2*actor.y))
				{
					if((p.y + p.height + 3) > actor.y && (p.y <(actor.y+ actor.height))&&  ( p.x + p.width/1.4 ) > actor.x && (p.x) < (actor.x+actor.width/1.4 ))
					{
						if(p.rand==9 || p.rand == 7)
						{
							
							EarthCount += 1;
							showEarthcount();



						}
						else
						{
							console.log(p);
							Pause=1;
							gameOverShow();
						}
						
					}
				}
			loop();
			showScore();

		});
	}
}


function gameStart()
{
	showScore()
	showLevel();
    actor.draw();
    window.setTimeout(drawHurdle,2000);
    window.setInterval(updateHurdles,Gspeed);
}
function gameRefresh()
{
	 Gspeed+=40;
	 ScoreMult +=5;
	 Level+=1;

showScore();
showLevel();
	actor.draw();
	var a=confirm("Start New level ?");
	if (a==false)
		{
			gameOverShow();
			return;
		}
	window.setTimeout(drawHurdle,100);

	window.setInterval(updateHurdles,Gspeed);
}


 function gameOverShow()
 {
    document.getElementById("Canvas").style.display= "none";
    document.getElementById("ScoreBoard").style.display= "none";
    document.getElementById("GameOver").style.display="block";
    document.getElementById("distance").innerHTML = score;
    document.getElementById("Score").innerHTML= EarthCount;
 }

function showScore()
{
	var scoreBoard=document.getElementById('showScore');
	scoreBoard.innerHTML = "Distance moved:" + score;
}
function showLevel()
{
	var LevelBoard= document.getElementById('showLevel');
	LevelBoard.innerHTML="Level: "+Level;
}

function showEarthcount()
{
	var LevelBoard= document.getElementById('EarthCount');
	LevelBoard.innerHTML="Score: " + EarthCount;
}
gameStart();






